home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 282 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.1 KB

  1. Path: news.seanet.com!usenet
  2. From: mitchell@seanet.com (where am i?)
  3. Newsgroups: comp.lang.c
  4. Subject: my atoi function, could someone suggest...
  5. Date: Wed, 03 Jan 1996 12:18:50 GMT
  6. Organization: Seanet Online Services, Seattle WA
  7. Message-ID: <4cf7ap$q4u@kaleka.seanet.com>
  8. NNTP-Posting-Host: mitchell.seanet.com
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. Hello.  This is my first attempt at an atoi function.  Although it
  12. works and behaves just like atoi, I was wondering if anyone would care
  13. for a look and make suggestions back to me about its efficiency.
  14.  
  15. int atoi ( char *change )
  16. {
  17.     int newint = 0, sign = 1;
  18.  
  19.     while ( *change )
  20.     {
  21.         if ( (*change < '0' || *change > '9') && *change != '-' )
  22.         {
  23.             *change++;
  24.         }
  25.         else
  26.         {
  27.             if ( *change == '-' )
  28.             {
  29.                 *change++;
  30.                 if ( *change >= '0' && *change <= '9' )
  31.                     sign = -1;
  32.             }
  33.             if ( *change >= '0' && *change <= '9' )
  34.             {
  35.                 while ( sign )
  36.                 {
  37.                     newint *= 10;
  38.                     newint = newint + *change - 48;
  39.                     *change++;
  40.                     if ( *change < '0' || *change > '9' )
  41.                     {
  42.                         return (  newint * sign );
  43.                         exit (0);
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.     }
  49.     return ( 0 );
  50. }
  51.  
  52. Thank you all very much.
  53.  
  54. Mitch
  55.  
  56.